$(function () { 
    var $form = $("form");
    $form.bootstrapValidator({
        feedbackIcons: {
            //如果验证通过，显示的小图标
            valid: 'glyphicon glyphicon-ok',
            //验证不通过
            invalid: 'glyphicon glyphicon-remove',
            //正在验证的状态
            validating: 'glyphicon glyphicon-refresh'
        },
        fields:{
            username:{
                validators:{
                    notEmpty:{
                        message:"用户名不能为空"
                    },
                    stringLength: {
                        min: 4,
                        max: 30,
                        message: '用户名长度必须在4到30之间'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: '用户名由数字字母下划线和.组成'
                    },
                    callback:{
                        message:'用户名错误'
                    }
                } 
            },
            password:{
                validators:{
                    notEmpty:{
                        message:"密码不能为空"
                    },
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: '密码长度必须在6到30之间'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: '密码由数字字母下划线和.组成'
                    },
                    callback:{
                        message:'密码错误'
                    }
                } 
            }
        }
    })

    $form.on('success.form.bv', function (e) {
        e.preventDefault();
        //使用ajax提交逻辑
        // alert('hjhc');
        // console.log($form.serialize());
        $.ajax({
            url:'/employee/employeeLogin',
            type:'POST',
            data:$form.serialize(),
            success:function(data){
                if(data.success){
                    console.log(window.history);
                    var href = window.sessionStorage.getItem('href');
    
                    if(href){
                      window.location.href = window.sessionStorage.getItem('href');
                      window.sessionStorage.removeItem('href');
                    }else {
                        window.location.href='index.html';
                    }

                }


                if(data.error==1000){
                    var validator = $form.data('bootstrapValidator');
                    validator.updateStatus('username', 'INVALID', 'callback');
                }
                if(data.error==1001){
                    var validator = $form.data('bootstrapValidator');
                    validator.updateStatus('password', 'INVALID', 'callback');
                }
            }
        })
    });

 })